using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Deepcode.Flux.Core.Systems.Addins;
using Deepcode.Flux.Core.Extensions;

public partial class _default : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	public override bool CheckFluxConfiguredOnStart { get { return true; } }

	protected void Page_Load(object sender, EventArgs e)
	{
		ArrayList coreUpdates = new ArrayList();
		ArrayList noncoreUpdates = new ArrayList();
		KnownAddins ka = new KnownAddins();

		// Check to see if any of the addin's require updating
		foreach (Addin a in AddinTree.Tree.UpdatableAddins)
		{
			bool bNeedsUpdate = false;
			// Determine if it needs updating - requires a GUID, a version and an install update script
			if ((a.CheckUpdatesAtLogin) &&
				(a.GUID != Guid.Empty) &&
				(a.InstallUpdateScript != String.Empty))
			{
				Version dbVersion = ka.GetDbVersionFor(a.GUID);
				Version xmlVersion = new Version(a.Version);
				if (xmlVersion > dbVersion) bNeedsUpdate = true;
			}
			// Now, add it to the appropriate list
			if (bNeedsUpdate)
			{
				if (a.IsCoreAddin) coreUpdates.Add(a);
				else noncoreUpdates.Add(a);
			}
		}

		// Process updates
		if (coreUpdates.Count > 0)
		{
			string ManifestFile = ((Addin) coreUpdates[0]).InstallUpdateScript;
			if( ManifestFile.EndsWith(".setup.config"))
				Response.Redirect( "~/admin/setup/process/default.aspx?ifile=" + ManifestFile, true );
			else
				Response.Redirect(ManifestFile, true);
		}
		if (noncoreUpdates.Count > 0)
		{
			string ManifestFile = ((Addin)noncoreUpdates[0]).InstallUpdateScript;
			if (ManifestFile.EndsWith(".setup.config"))
				Response.Redirect("~/admin/setup/process/default.aspx?ifile=" + ManifestFile, true);
			else
				Response.Redirect(ManifestFile, true);
		}

		// Launch the primary UI
		AddinTreeNode uiNode = Deepcode.Flux.Core.Extensions.AddinTree.Tree.GetTreeNode("/Flux/Menus/");
		string pui = (string) uiNode.BuildChildItem("primaryUI", null);
		if( pui == null ) pui = "~/admin/menu";
		Response.Redirect(pui, true);
	}
}
